package org.delphy.indy2.containers
{
	import caurina.transitions.Tweener;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.core.Application;
	import mx.managers.PopUpManager;
	
	import org.delphy.indy2.containers.carbonWindowClasses.*;
	
	
	[Event("windowCloseEvent")]
	[Event("windowStartMoveEvent")]
	[Event("windowStopMoveEvent")]
	[Event("windowMovingEvent")]
	
	public class CarbonWindow extends CarbonSlimPanel
	{
		
		public var transitionTime:Number=1.3;
		
		private var _titleBar:CarbonTitleBar;
		
		private var _titleDirection:String=CarbonTitleBar.HORIZONTAL_DIRECTION;
		
		public static const WINDOW_CLOSE_EVENT:String="windowCloseEvent";
		public static const WINDOW_START_MOVE_EVENT:String="windowStartMoveEvent";
		public static const WINDOW_STOP_MOVE_EVENT:String="windowStopMoveEvent";
		public static const WINDOW_MOVING_EVENT:String="windowMovingEvent";

		public function get titleDirection():String
		{ 
			return _titleDirection; 
		}

		public function set titleDirection(value:String):void
		{
			if (value != _titleDirection)
			{
				_titleDirection = value;
				
				invalidateDisplayList();
			}
		}
		
		private var _titleWeight:Number=30;

		public function get titleWeight():Number
		{ 
			return _titleWeight; 
		}

		public function set titleWeight(value:Number):void
		{
			if (value != _titleWeight)
			{
				_titleWeight = value;
				invalidateDisplayList();
			}
		}
		
		private var _title:String;

		public function get title():String
		{ 
			return _title; 
		}

		public function set title(value:String):void
		{
			if (value !== _title)
			{
				_title = value;
				invalidateDisplayList();
			}
		}
		
		private var isMoving:Boolean=false;
		private var dragStartX:Number;
		private var dragStartY:Number;
		private var startX:Number;
		private var startY:Number;
		
		public function CarbonWindow()
		{
			super();
			
		}
		
		override protected function createChildren():void{
			super.createChildren();
			
			_titleBar=new CarbonTitleBar();
			rawChildren.addChild(_titleBar);
			
			_titleBar.addEventListener(MouseEvent.MOUSE_DOWN,onTitleMouseDown);
//			_titleBar.closeButton.addEventListener(MouseEvent.CLICK,onCloseButtonClick);
		}
		
		private function onCloseButtonClick(evt:MouseEvent):void{
			evt.stopPropagation();
			
			dispatchEvent(new Event(WINDOW_CLOSE_EVENT));
			
			parent.removeChild(this);
		}
		
		private function onTitleMouseDown(evt:MouseEvent):void{
			systemManager.addEventListener(MouseEvent.MOUSE_UP,onTitleMouseUp,true);
			systemManager.addEventListener(MouseEvent.MOUSE_MOVE,onTitleMouseMove,true);
			
			dragStartX=evt.stageX;
			dragStartY=evt.stageY;
			startX=x;
			startY=y;
			
			isMoving=true;
			
			dispatchEvent(new Event(WINDOW_START_MOVE_EVENT));
		}
		
		private function onTitleMouseUp(evt:MouseEvent):void{
			if(isMoving){
				evt.stopImmediatePropagation();
				move(startX+evt.stageX-dragStartX,startY+evt.stageY-dragStartY);
				
				systemManager.removeEventListener(MouseEvent.MOUSE_UP,onTitleMouseUp);
				systemManager.removeEventListener(MouseEvent.MOUSE_MOVE,onTitleMouseMove);
				
				isMoving=false;			
				
				dispatchEvent(new Event(WINDOW_STOP_MOVE_EVENT));	
			}
		}
		
		private function onTitleMouseMove(evt:MouseEvent):void{
			if(isMoving){
				evt.stopImmediatePropagation();
				move(startX+evt.stageX-dragStartX,startY+evt.stageY-dragStartY);
				
				dispatchEvent(new Event(WINDOW_MOVING_EVENT));
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);

			_titleBar.x=_border.weight;
			_titleBar.y=_border.weight;
			_titleBar.title=_title;
			
			// Reposition the title bar and the client area
			if(_titleDirection==CarbonTitleBar.HORIZONTAL_DIRECTION){
				_titleBar.width=width-_border.weight*2;
				_titleBar.height=titleWeight;
				_titleBar.topLeftRadius=_border.cornerRadius;
				_titleBar.topRightRadius=_border.cornerRadius;
				_titleBar.bottomLeftRadius=0;
				_titleBar.bottomRightRadius=0;
				_titleBar.direction=_titleDirection;
				
				_content.x=_border.weight;
				_content.y=_titleBar.y+_titleBar.height;
				_content.height=_content.height-_titleBar.height;
				_content.setStyle("topLeftRadius",0);
				_content.setStyle("topRightRadius",0);
				
			}
			else if(_titleDirection==CarbonTitleBar.VERTICAL_DIRECTION){
				_titleBar.width=titleWeight;
				_titleBar.height=height - _border.weight * 2;
				_titleBar.topLeftRadius=_border.cornerRadius;
				_titleBar.bottomLeftRadius=_border.cornerRadius;
				_titleBar.topRightRadius=0;
				_titleBar.bottomRightRadius=0;
				_titleBar.direction=_titleDirection;
				
				_content.x=_titleBar.x+_titleBar.width;
				_content.y=_border.weight;
				_content.width=_content.width-_titleBar.width;
				_content.setStyle("topLeftRadius",0);
				_content.setStyle("bottomLeftRadius",0);
			}
			
		}
		
		public function popUp(center:Boolean=true):void{
			
			if(Tweener.isTweening(this)){
				return;
			}
						
			alpha=0;
			PopUpManager.addPopUp(this,DisplayObject(Application.application),false);
			if(center){
				PopUpManager.centerPopUp(this);
			}	
			
			Tweener.addTween(this,{alpha:1,time:transitionTime});
			
		}
		
		public function close():void{
			Tweener.addTween(this,{alpha:0,time:transitionTime,onComplete:onCloseAnimationComplete});
			
		}
		
		private function onCloseAnimationComplete():void{
			PopUpManager.removePopUp(this);
		}
		
	}
}